<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="订单号" prop="workOrderNo">
                <el-input v-model="queryParams.workOrderNo" placeholder="请输入订单号" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="物料编号" prop="materialCode">
                <el-input v-model="queryParams.materialCode" placeholder="请输入物料编号" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="物料名称" prop="materialName">
                <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="任务号" prop="taskNo">
                <el-input v-model="queryParams.taskNo" placeholder="请输入物料编号" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="缺料告警" prop="taskPlanStatus">
                <el-select v-model="queryParams.shortage" placeholder="请选择">
                    <el-option v-for="item in alarmOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="materialList">
            <el-table-column label="序号" type="index" width="50" />
            <el-table-column label="订单号" prop="workOrderNo" width="auto" />
            <el-table-column label="任务号" prop="taskNo" width="auto" />
            <el-table-column label="物料编号" prop="materialCode" :show-overflow-tooltip="true" width="auto" />
            <el-table-column label="物料名称" prop="materialName" :show-overflow-tooltip="true" width="auto" />
            <el-table-column label="配送位置" prop="deliveryPosName" :show-overflow-tooltip="true" width="auto" />
            <el-table-column label="需求数量" prop="requiredQty" :show-overflow-tooltip="true" width="auto" />
            <el-table-column label="产线库存数" prop="stockQty" :show-overflow-tooltip="true" width="auto" />
            <el-table-column label="安全库存" prop="safeStockQty" :show-overflow-tooltip="true" width="auto" />
            <el-table-column label="缺料告警" prop="shortage" :show-overflow-tooltip="true" width="auto">
                <template slot-scope="scope">
                    <span>{{ scope.row.shortage == 0 ?'否': (scope.row.shortage == 1 ?'是':'')}}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" prop="shortage" :show-overflow-tooltip="true" width="auto">
                <template slot-scope="scope">
                    <el-button :disabled="scope.row.stockModifyFlag != 1" size="mini" type="text" @click="handleAdjust(scope.row)">库存调整</el-button>
                </template>
            </el-table-column>


        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <el-dialog title="库存调整" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form ref="form" :model="materialData" label-width="100px" :rules="rules">
                <el-form-item label="物料名称">
                    <el-input v-model="materialData.materialName" disabled="true" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="物料编码">
                    <el-input v-model="materialData.materialCode" disabled="true" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="产线库存数">
                    <el-input v-model="materialData.stockQty" disabled="true" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="库存调整" prop="newStockQty">
                    <el-input v-model="materialData.newStockQty"  placeholder="请输入库存"></el-input>
                </el-form-item>
                
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="adjustStorage">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { getMaterial, updateStock } from "@/api/lineProduction/index";

    export default {
        name: "Role",
        data() {
            return {
                // 遮罩层
                loading: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 计划表格数据
                materialList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 日期范围
                dateRange: ['', ''],
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10
                },
                // 是否缺料告警
                alarmOptions: [
                    {
                        label: '是',
                        value: 1
                    },
                    {
                        label: '否',
                        value: 0
                    }
                ],
                dialogVisible: false,
                materialData: {
                    newStockQty: ''
                },
                rules: {
                    newStockQty: [
                        { required: true, message: "库存不能为空", trigger: ["change",'blur'] }
                    ]
                },

            };
        },
        created() {
            this.getList()
        },
        methods: {
            /** 搜索按钮操作 */
            handleQuery() {
                // this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.dateRange = [];
                this.resetForm("queryForm");
                this.queryParams = {};
                this.handleQuery();
            },
            getList() {
                getMaterial(this.queryParams).then(res => {
                    this.materialList = res.rows
                    this.total = res.total
                })
            },
            handleAdjust(row) {
                console.log(row);
                
                this.materialData = row
                console.log(this.materialData);
                
                this.dialogVisible = true
            },
            adjustStorage() {
                let param = {
                    id: this.materialData.id,
                    stockQty: this.materialData.newStockQty
                }
                this.$refs.form.validate(valid => {
                    if (valid) {
                        updateStock(param).then(res => {
                            this.$message.success(res.msg)
                            this.getList()
                            this.dialogVisible = false
                        })
                    }
                })

            },
            cancel() {
                this.dialogVisible = false
                this.materialData.newStockQty = ''
            },
            handleClose() { }
        }
    };
</script>